package com.jsmile.cloud.accountcenter.account.service;

import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.accountcenter.account.model.UserAddress;
import com.jsmile.cloud.accountcenter.account.repo.UserAddressRepository;
import com.jsmile.cloud.accountcenter.account.repo.UserAddressSearchRepository;
import com.jsmile.cloud.accountcenter.api.enums.AccountCodeEnum;
import com.jsmile.cloud.accountcenter.api.enums.HasDefaultEnum;
import com.jsmile.cloud.accountcenter.api.exception.AccountException;
import com.jsmile.cloud.accountcenter.api.req.address.ReqAddressDetial;
import com.jsmile.cloud.accountcenter.api.req.address.ReqAddressPageQuery;
import com.jsmile.cloud.accountcenter.api.req.address.ReqAddressSave;
import com.jsmile.cloud.accountcenter.api.req.address.ReqAddressUpdate;
import com.jsmile.cloud.accountcenter.api.res.ResUserAddress;
import com.jsmile.mall.api.enums.JSmileCacheEnum;
import com.jsmile.mall.api.exception.ExceptionKit;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@AllArgsConstructor
public class UserAddressApplicationService {

    private final UserAddressRepository userAddressRepository;
    private final UserAddressSearchRepository userAddressSearchRepository;

    @Cacheable(value = JSmileCacheEnum.JSmileCacheName.oneMonth, key = "'address_'+#addressId")
    public ResUserAddress getUserAddress(long addressId) {
        log.info("获取用户地址:{]", addressId);
        UserAddress userAddress = userAddressSearchRepository.getById(addressId);
        if (null == userAddress) {
            throw new AccountException(AccountCodeEnum.ADDRESS_NOT_EXISTS);
        }
        return BeanUtil.toBean(userAddress, ResUserAddress.class);
    }

    public Boolean addUserAddress(ReqAddressSave reqAddressSave) {
        UserAddress userAddress = UserAddress.create(reqAddressSave);
        log.info("地址创建:{]", userAddress);
        return userAddressRepository.save(userAddress);
    }

    public Boolean changeUserAddress(ReqAddressUpdate reqAddressUpdate) {
        UserAddress userAddress = userAddressSearchRepository.getById(reqAddressUpdate.getAddressId());
        log.info("修改地址:{]", reqAddressUpdate);
        userAddress.changeUserAddress(reqAddressUpdate);
        return userAddressRepository.updateById(userAddress);
    }

    public ResUserAddress getDefaultUserAddress(long userId) {
        log.info("获取默认地址:{]", userId);
        List<UserAddress> userAddressess = userAddressSearchRepository
            .list(new QueryWrapper<UserAddress>().lambda().eq(UserAddress::getUserId, userId).eq(UserAddress::getHasDefault, HasDefaultEnum.DEFAULT.getValue()));
        return CollUtil.isEmpty(userAddressess) ? null : BeanUtil.toBean(userAddressess.get(0), ResUserAddress.class);
    }

    public Boolean deleteUserAddress(ReqAddressDetial reqAddressDelete) {
        UserAddress userAddress = userAddressSearchRepository.getById(reqAddressDelete.getAddressId());
        userAddress.deleteUserAddress();
        log.info("地址删除:{]", reqAddressDelete);
        return userAddressRepository.removeById(reqAddressDelete.getAddressId());
    }

    public IPage<ResUserAddress> getUserAddressPageList(ReqAddressPageQuery reqAddressPageQuery) {
        ExceptionKit.checkNotNull(reqAddressPageQuery.getCurTenantId(), "租户id不能为空!");
        log.info("地址查询:{]", reqAddressPageQuery);
        IPage<UserAddress> pages = userAddressSearchRepository.pageQuery(reqAddressPageQuery);
        IPage<ResUserAddress> res = pages.convert(x -> BeanUtil.toBean(x, ResUserAddress.class));
        return res;
    }

}
